package com.itboye.boyeshoping.activity

import android.content.Intent
import android.os.Bundle
import android.text.Editable
import android.text.TextWatcher
import android.view.View
import android.widget.*

import com.itboye.boyeshoping.R
import com.itboye.boyeshoping.adapter.GoodsListAdapter
import com.itboye.boyeshoping.base.BaseActivity

/**
 * Created by Administrator on 2018/6/28 0028.
 */

class SearchGoodsActivity : BaseActivity() , View.OnClickListener {


    private lateinit var searchEt:EditText
    private lateinit var searchDeleteIv:ImageView

    private lateinit var searchOrCancelTv:TextView
    private lateinit var hotSearchGv:GridView

    private lateinit var hotSearchLl:LinearLayout

    //商品列表

    private lateinit var defeault:TextView
    private lateinit var salesNums:TextView
    private lateinit var price:TextView
    private lateinit var goodsLv:ListView

    private lateinit var searchGoodsLl:LinearLayout


    private lateinit var adapter:GoodsListAdapter

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_searchgoods)
        initFindView()
    }

    private fun initFindView() {
        searchEt= findViewById(R.id.searchEt)
        searchDeleteIv= findViewById(R.id.searchDeleteIv)

        //热点搜索
        searchOrCancelTv= findViewById(R.id.searchOrCancelTv)
        hotSearchGv= findViewById(R.id.hotSearchGv)
        hotSearchLl= findViewById(R.id.hotSearchLl)

        //商品列表
        defeault= findViewById(R.id.defeault)
        salesNums= findViewById(R.id.salesNums)
        price= findViewById(R.id.price)
        goodsLv= findViewById(R.id.goodsLv)
        searchGoodsLl= findViewById(R.id.searchGoodsLl)

        defeault.setOnClickListener(this)
        price.setOnClickListener(this)
        salesNums.setOnClickListener(this)
        searchDeleteIv.setOnClickListener(this)
        searchOrCancelTv.setOnClickListener(this)

        adapter=GoodsListAdapter(this)
        goodsLv.adapter=adapter
        searchEt.addTextChangedListener(object : TextWatcher {

            //onTextChanged(CharSequence s, int start, int before, int count)
            //beforeTextChanged(CharSequence s, int start, int count, int after) {
            override fun beforeTextChanged(charSequence: CharSequence, i: Int, i1: Int, i2: Int) {

            }

            override fun onTextChanged(charSequence: CharSequence, i: Int, i1: Int, i2: Int) {
             if(charSequence.isNotEmpty()){
                 searchOrCancelTv.text = "搜索"
             }else{

                 searchOrCancelTv.text = "取消"
             }
            }

            override fun afterTextChanged(editable: Editable) {

            }
        })
    }

    override fun onClick(p0: View?) {

        when (p0!!.id){
             R.id.searchDeleteIv ->{
               searchEt.setText("")
             }
            R.id.searchOrCancelTv->{

                if (searchOrCancelTv.text == "搜索"){
                   //商品列表显示
                    searchGoodsLl.visibility=View.VISIBLE
                    hotSearchLl.visibility=View.GONE
                }else{
                    this.finish()
                }
            }
            R.id.defeault->{//默认
                clearState()
                defeault.setTextColor(resources.getColor(R.color.maincolor))
                //请求数据
            }

            R.id.salesNums->{//销量
                clearState()
                salesNums.setTextColor(resources.getColor(R.color.maincolor))

            }

            R.id.price->{ //价格
                clearState()
                price.setTextColor(resources.getColor(R.color.maincolor))
            }
        }
    }

    private fun clearState() {
     defeault.setTextColor(resources.getColor(R.color.taobaotext))
        salesNums.setTextColor(resources.getColor(R.color.taobaotext))
        price.setTextColor(resources.getColor(R.color.taobaotext))

    }


}
